package com.lcx.hw.service.impl;

import com.lcx.hw.dao.TokenDao;
import com.lcx.hw.dao.UserDao;
import com.lcx.hw.pojo.User;
import com.lcx.hw.pojo.Token;
import com.lcx.hw.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    public static final String DATE_STR = "yyyy-MM-dd HH:mm:ss";

    @Autowired
    private UserDao userDao;

    @Autowired
    private TokenDao tokenDao;

    @Override
    public Integer register(String email, String password, String code) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_STR);
        Date now = new Date();
        User user = (User) getVoByEmail(email);
        //如果是注册功能
        if (user != null && !"0".equals(password)){
            //判断是否超时
            Date parse = sdf.parse(user.getExpireTime());
            if (parse.before(now)){
                return 2;
            }
            if (!user.getCode().equals(code)){
                return 1;
            }
        }
        Date afterDate = new Date(now.getTime() + 600000);
        if (user == null){
            user = new User();
        }
        try {
            user.setCode(code)
                    .setEmail(email)
                    .setPassword(password)
                    .setCreateTime(sdf.format(now))
                    .setExpireTime(sdf.format(afterDate));
            userDao.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return 3;
        }
        return 0;
    }

    @Override
    public boolean isRegistered(String email) throws Exception{
        User authCode = new User();
        authCode.setEmail(email);
        Example<User> example = Example.of(authCode);
        Optional<User> one = userDao.findOne(example);
        if (one.isPresent()){
            User user = one.get();
            //有密码代表注册过
            return  user.getPassword() != null;
        }
        return false;
    }

    @Override
    public String login(String email, String password) {
        User authCode = new User();
        authCode.setEmail(email);
        Example<User> example = Example.of(authCode);
        Optional<User> one = userDao.findOne(example);
        String info = info(email);
        if (StringUtils.isBlank(info)){
            return "false";
        }
        if (one.isPresent()){
            User code = one.get();
            if (code.getPassword().equals(password)){
                return info;
            }
        }
        return "false";
    }

    @Override
    public String info(String token) {
        Token t = new Token();
        t.setEmail(token);
        Example<Token> example = Example.of(t);
        Optional<Token> one = tokenDao.findOne(example);
        if (one.isPresent()){
            return one.get().getToken();
        }
        return null;
    }

    @Override
    public Object getVoByEmail(String email) {
        User authCode = new User();
        authCode.setEmail(email);
        Example<User> example = Example.of(authCode);
        Optional<User> one = userDao.findOne(example);
        if (one.isPresent()){
            return one.get();
        }
        return null;
    }

    @Override
    public String saveToken(String email) throws Exception{
        String uuid = UUID.randomUUID().toString();
        Token token = new Token();
        token.setToken(uuid);
        token.setEmail(email);
        tokenDao.save(token);
        return uuid;
    }
}
